<?php

namespace Hilaoyu\LaravelExtend\Providers;

use Hilaoyu\LaravelExtend\Providers\RequestMacroServiceProvider;
use Hilaoyu\LaravelExtend\Services\AccessControl\AbstractModelAccessControlEntities;
use Hilaoyu\LaravelExtend\Services\Auth\AuthService;
use Hilaoyu\LaravelExtend\Services\Auth\Redefinition\DatabaseUserWithMultipleAuthTokenProvider;
use Hilaoyu\LaravelExtend\Services\Auth\Redefinition\EloquentUserWithMultipleAuthTokenProvider;
use Hilaoyu\LaravelExtend\Supports\SubApp;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Event;


class AccessControlServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerRoutes();
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {

        $this->registerGate();

    }


    protected function registerRoutes()
    {

        $route_config = config('access_control.route', []);

        $accessControlController = data_get($route_config, 'controller', '');
        if(!$accessControlController){
            return;
        }

        data_set($route_config, 'name', 'l-e-s.access-control');
        data_set($route_config, 'prefix', data_get($route_config, 'prefix', '_les/access-control'));


        SubApp::registerRoutes(function () use ($route_config,$accessControlController) {
            \Route::post('role-save', $accessControlController . '@roleSave')
                ->defaults('_view', '_json_')
                ->name('.role.save');
            \Route::post('role-delete', $accessControlController . '@roleDelete')
                ->defaults('_view', '_json_')
                ->name('.role.delete');
            \Route::get('role-accesses', $accessControlController . '@roleAccesses')
                ->defaults('_view', '_json_')
                ->name('.role.accesses.get');
            \Route::post('role-accesses', $accessControlController . '@roleAccessesSave')
                ->defaults('_view', '_json_')
                ->name('.role.accesses.save');
            \Route::post('user-roles', $accessControlController . '@userRolesSave')
                ->defaults('_view', '_json_')
                ->name('.user.roles.save');
            \Route::post('superusers', $accessControlController . '@superusersSave')
                ->defaults('_view', '_json_')
                ->name('.superusers.save');
            \Route::post('superusers/delete', $accessControlController . '@superusersDelete')
                ->defaults('_view', '_json_')
                ->name('.superusers.delete');
        }, '', $route_config, 'api');
    }

    public function registerGate()
    {
        $actions = collect(AbstractModelAccessControlEntities::GetAllowActions())->collapse()->keys();
        $actions->push('view');
        $actions->push('edit');
        $actions->push('delete');

        foreach ($actions->toArray() as $action) {
            Gate::define($action, function ($user, $identifier = '') use ($action) {
                try {
                    //dd($user,$identifier);
                    if (config('app.debug', false)) {
                        //comResponse()->withDebug([$user, $action, $identifier], 'Gate_define');
                    }
                    return $user->checkAccess($identifier, $action);
                } catch (\Exception $e) {

                    if (config('app.debug', false)) {
                        comResponse()->withDebug([
                            'exception' => $e->getMessage(),
                            'user' => $user,
                            'action' => $action,
                            'identifier' => $identifier,
                        ], 'gate_check');
                    }
                    return false;
                }
                return false;
            });
        }

    }
}
